package com.enjoy.xbase.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.Xfermode;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;

import androidx.appcompat.widget.AppCompatImageView;

import com.enjoy.xbase.R;

import java.lang.ref.WeakReference;

/**
 * 自定义ImageView
 * 圆角、圆形
 *
 * @author plf
 */
@Deprecated
public class EjImageView extends AppCompatImageView {
    private static final int TYPE_CIRCLE = 0;
    private static final int TYPE_ROUND = 1;
    private static final int BODER_RADIUS_DEFAULT = 10;

    private Paint mPaint;
    private Xfermode mXfermode = new PorterDuffXfermode(Mode.DST_IN);
    private Bitmap mMaskBitmap;

    private WeakReference<Bitmap> mWeakBitmap;
    private int type;
    private int mBorderRadius;

    public EjImageView(Context context) {
        this(context, null);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
    }

    public EjImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.EjImageView);
        type = a.getInt(R.styleable.EjImageView_type, TYPE_CIRCLE);
        mBorderRadius = a.getDimensionPixelSize(R.styleable.EjImageView_radius, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, BODER_RADIUS_DEFAULT, getResources().getDisplayMetrics()));// 榛樿锟�?0dp
        Log.e("TAG", mBorderRadius + "");
        a.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (type == TYPE_CIRCLE) {
            int width = Math.min(getMeasuredWidth(), getMeasuredHeight());
            setMeasuredDimension(width, width);
        }
    }

    @Override
    public void invalidate() {
        mWeakBitmap = null;
        if (mMaskBitmap != null) {
            mMaskBitmap.recycle();
            mMaskBitmap = null;
        }
        super.invalidate();
    }

    @Override
    public void setImageBitmap(Bitmap bm) {
        super.setImageBitmap(bm);
        invalidate();
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {

        //Bitmap bitmap = mWeakBitmap == null ? null : mWeakBitmap.get();
        Bitmap bitmap;
        Drawable drawable = getDrawable();
        if (drawable != null) {
            int dWidth = drawable.getIntrinsicWidth();
            int dHeight = drawable.getIntrinsicHeight();
            bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Config.ARGB_8888);
            float scale;
            Canvas drawCanvas = new Canvas(bitmap);
            if (type == TYPE_ROUND) {
                scale = Math.max(getWidth() * 1.0f / dWidth, getHeight() * 1.0f / dHeight);
            } else {
                scale = getWidth() * 1.0F / Math.min(dWidth, dHeight);
            }
            drawable.setBounds(0, 0, (int) (scale * dWidth), (int) (scale * dHeight));
            drawable.draw(drawCanvas);
            if (mMaskBitmap == null || mMaskBitmap.isRecycled()) {
                mMaskBitmap = getBitmap();
            }
            mPaint.reset();
            mPaint.setFilterBitmap(false);
            mPaint.setXfermode(mXfermode);
            drawCanvas.drawBitmap(mMaskBitmap, 0, 0, mPaint);
            mPaint.setXfermode(null);
            canvas.drawBitmap(bitmap, 0, 0, null);
            mWeakBitmap = new WeakReference<>(bitmap);
        } else {
            super.onDraw(canvas);
        }
    }

    public Bitmap getBitmap() {
        Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.BLACK);
        if (type == TYPE_ROUND) {
            canvas.drawRoundRect(new RectF(0, 0, getWidth(), getHeight()), mBorderRadius, mBorderRadius, paint);
        } else {
            canvas.drawCircle(getWidth() / 2, getWidth() / 2, getWidth() / 2, paint);
        }
        return bitmap;
    }
}
